<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-17
 * Time: 23:34
 */

namespace app\base;


use think\Cookie;
use think\Session;

class Auth
{
    /***
     * 执行登录
     * @param $user
     * @param bool $remember
     * @return bool
     */
    public static function doLogin($user,$remember=false)
    {
        if (empty($user)) {
            return false;
        }
        if ($remember) {
            Cookie::set('user', $user);
        }else{
            if (Cookie::has('user')) {
                Cookie::delete('user');
            }
        }
        Session::set('user', $user);
        return true;
    }

    /***
     * 检测登录状态
     * @return bool
     */
    public static function isLogin()
    {
        if (is_null(Session::get('user'))) {
            if (Cookie::has('user')) {
                Session::set('user', Cookie::get('user'));
                return true;
            }
            return false;
        }
        return true;
    }

    /***
     * 退出登录
     * @return bool
     */
    public static function outLogin()
    {
        Session::delete('user');
        if (Cookie::has('user')) {
            Cookie::delete('user');
        }
        return true;
    }

}